# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>
# Contributor: Marcin Sielski <marcin.sielski@motorolasolutions.com>

_realname=gst-plugins-bad
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-gst-plugins-bad-libs"
         "${MINGW_PACKAGE_PREFIX}-gst-plugins-bad"
         "${MINGW_PACKAGE_PREFIX}-gst-plugin-opencv")
pkgver=1.22.5
pkgrel=1
pkgdesc="GStreamer Multimedia Framework Bad Plugins (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://gstreamer.freedesktop.org/"
license=("spdx:LGPL-2.1-or-later")
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-meson"
  "${MINGW_PACKAGE_PREFIX}-ninja"
  "${MINGW_PACKAGE_PREFIX}-pkg-config"
  "${MINGW_PACKAGE_PREFIX}-gst-plugins-base"
  "${MINGW_PACKAGE_PREFIX}-gobject-introspection"
  "${MINGW_PACKAGE_PREFIX}-shaderc"
  "${MINGW_PACKAGE_PREFIX}-vulkan-loader"
  "${MINGW_PACKAGE_PREFIX}-gtk-doc"
  "${MINGW_PACKAGE_PREFIX}-aom"
  "${MINGW_PACKAGE_PREFIX}-bzip2"
  "${MINGW_PACKAGE_PREFIX}-cairo"
  "${MINGW_PACKAGE_PREFIX}-chromaprint"
  "${MINGW_PACKAGE_PREFIX}-curl"
  "${MINGW_PACKAGE_PREFIX}-faad2"
  "${MINGW_PACKAGE_PREFIX}-faac"
  "${MINGW_PACKAGE_PREFIX}-fdk-aac"
  "${MINGW_PACKAGE_PREFIX}-fluidsynth"
  "${MINGW_PACKAGE_PREFIX}-gsm"
  "${MINGW_PACKAGE_PREFIX}-gtk3"
  "${MINGW_PACKAGE_PREFIX}-lcms2"
  "${MINGW_PACKAGE_PREFIX}-libass"
  "${MINGW_PACKAGE_PREFIX}-libbs2b"
  "${MINGW_PACKAGE_PREFIX}-libdca"
  "${MINGW_PACKAGE_PREFIX}-libdvdnav"
  "${MINGW_PACKAGE_PREFIX}-libdvdread"
  "${MINGW_PACKAGE_PREFIX}-libexif"
  "${MINGW_PACKAGE_PREFIX}-libgme"
  "${MINGW_PACKAGE_PREFIX}-libjpeg"
  "${MINGW_PACKAGE_PREFIX}-libmodplug"
  "${MINGW_PACKAGE_PREFIX}-libmpeg2"
  "${MINGW_PACKAGE_PREFIX}-libnice"
  "${MINGW_PACKAGE_PREFIX}-librsvg"
  "${MINGW_PACKAGE_PREFIX}-libsndfile"
  "${MINGW_PACKAGE_PREFIX}-libsrtp"
  "${MINGW_PACKAGE_PREFIX}-libssh2"
  "${MINGW_PACKAGE_PREFIX}-libwebp"
  "${MINGW_PACKAGE_PREFIX}-libxml2"
  "${MINGW_PACKAGE_PREFIX}-libmicrodns"
  "${MINGW_PACKAGE_PREFIX}-libmpcdec"
  "${MINGW_PACKAGE_PREFIX}-nettle"
  "${MINGW_PACKAGE_PREFIX}-openal"
  "${MINGW_PACKAGE_PREFIX}-opencv"
  "${MINGW_PACKAGE_PREFIX}-openexr"
  "${MINGW_PACKAGE_PREFIX}-openh264"
  "${MINGW_PACKAGE_PREFIX}-openjpeg2"
  "${MINGW_PACKAGE_PREFIX}-openssl"
  "${MINGW_PACKAGE_PREFIX}-opus"
  "${MINGW_PACKAGE_PREFIX}-orc"
  "${MINGW_PACKAGE_PREFIX}-pango"
  "${MINGW_PACKAGE_PREFIX}-rtmpdump"
  "${MINGW_PACKAGE_PREFIX}-soundtouch"
  "${MINGW_PACKAGE_PREFIX}-spandsp"
  "${MINGW_PACKAGE_PREFIX}-srt"
  "${MINGW_PACKAGE_PREFIX}-vo-amrwbenc"
  "${MINGW_PACKAGE_PREFIX}-webrtc-audio-processing"
  "${MINGW_PACKAGE_PREFIX}-x265"
  "${MINGW_PACKAGE_PREFIX}-zbar"
)
source=("${url}/src/${_realname}/${_realname}-${pkgver}.tar.xz"{,.asc}
        "0001-meson-fix-vulkan.patch"
        "0002-fix-vulkan-mkenum.patch"
        "0003-enable-mediafoundation-plugin.patch"
        "0004-fix-clang-build.patch")
sha256sums=('e64e75cdafd7ff2fc7fc34e855b06b1e3ed227cc06fa378d17bbcd76780c338c'
            'SKIP'
            '000907bb77b31bd5717297aaa9f7ab7d01a0c9a0e7966fd05f077e786b8e652f'
            '5ca55fdfc4c5d10d2319f83dddac8130c2ffab67b1248d2233cdcb188d5a0fd3'
            '5cf2daa1b967112139e780a256957642cd005c25b1572e892d03021d1dec60ef'
            'cb1765e08198312150fa544998e556e42d434dc8fbc2cf962aac3e8ba1f55884')
validpgpkeys=('D637032E45B8C6585B9456565D2EEE6F6F349D7C') # Tim Müller <tim@gstreamer-foundation.org>

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -Np1 -i "${srcdir}/0001-meson-fix-vulkan.patch"
  patch -Np1 -i "${srcdir}/0002-fix-vulkan-mkenum.patch"

  # https://gitlab.freedesktop.org/gstreamer/gst-plugins-bad/-/merge_requests/1882
  # Update 1.22: disabled mediafoundation again due to new missing APIs
  patch -Np1 -i "${srcdir}/0003-enable-mediafoundation-plugin.patch"

  # This is already fixed in https://github.com/GPUOpen-LibrariesAndSDKs/AMF where
  # gstreamer copies it from, so we just have to wait for them to sync
  patch -Np1 -i "${srcdir}/0004-fix-clang-build.patch"
}

build() {
  [[ -d "${srcdir}/build-${MSYSTEM}" ]] && rm -rf "${srcdir}/build-${MSYSTEM}"
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/meson.exe setup \
    --prefix="${MINGW_PREFIX}" \
    --buildtype plain \
    --wrap-mode=nofallback \
    -Dauto_features=enabled \
    -Dpackage-origin='https://www.msys2.org' \
    -Dtests=disabled \
    -Ddoc=disabled \
    -Dexamples=disabled \
    -Dx11=disabled \
    -Dwayland=disabled \
    -Dbluez=disabled \
    -Ddvb=disabled \
    -Dfbdev=disabled \
    -Dipcpipeline=disabled \
    -Dkms=disabled \
    -Dmsdk=disabled \
    -Dopensles=disabled \
    -Dshm=disabled \
    -Dtinyalsa=disabled \
    -Duvch264=disabled \
    -Ddc1394=disabled \
    -Ddirectfb=disabled \
    -Dflite=disabled \
    -Dkate=disabled \
    -Dladspa=disabled \
    -Dlibde265=disabled \
    -Dlv2=disabled \
    -Dmpeg2enc=disabled \
    -Dmplex=disabled \
    -Dneon=disabled \
    -Dopenmpt=disabled \
    -Dopenni2=disabled \
    -Dsbc=disabled \
    -Dsctp=enabled \
    -Dteletext=disabled \
    -Dvoaacenc=disabled \
    -Dwildmidi=disabled \
    -Dwpe=disabled \
    -Diqa=disabled \
    -Dmagicleap=disabled \
    -Dv4l2codecs=disabled \
    -Dva=disabled \
    -Dwasapi2=disabled \
    -Davtp=disabled \
    -Dsvthevcenc=disabled \
    -Dzxing=disabled \
    -Dasio=disabled \
    -Dgpl=enabled \
    -Dgs=disabled \
    -Disac=disabled \
    -Dldac=disabled \
    -Donnx=disabled \
    -Dopenaptx=disabled \
    -Dqroverlay=disabled \
    -Ddirectshow=disabled \
    -Dwic=disabled \
    -Dgtk3=disabled \
    -Dmediafoundation=disabled \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/meson compile
}

package_gst-plugins-bad-libs() {
  depends=(
    "${MINGW_PACKAGE_PREFIX}-gst-plugins-base"
    "${MINGW_PACKAGE_PREFIX}-vulkan-loader"
  )
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/meson install

  rm -f ${pkgdir}${MINGW_PREFIX}/lib/gstreamer*/*.a

  # for gst-plugin-opencv
  mkdir -p plugin-opencv/bin/
  mkdir -p plugin-opencv/include/gstreamer-1.0/gst/
  mkdir -p plugin-opencv/lib/gstreamer-1.0/
  mv "${pkgdir}${MINGW_PREFIX}/bin/libgstopencv-1.0-0.dll" plugin-opencv/bin/
  mv "${pkgdir}${MINGW_PREFIX}/include/gstreamer-1.0/gst/opencv" plugin-opencv/include/gstreamer-1.0/gst/
  mv "${pkgdir}${MINGW_PREFIX}/lib/libgstopencv-1.0.dll.a" plugin-opencv/lib/
  mv "${pkgdir}${MINGW_PREFIX}/lib/gstreamer-1.0/libgstopencv.dll" plugin-opencv/lib/gstreamer-1.0/

  # things moved to the plugins package later
  mkdir -p _plugins/lib
  mv "${pkgdir}${MINGW_PREFIX}/lib/gstreamer-1.0" _plugins/lib
}

package_gst-plugins-bad() {
  depends=(
    "${MINGW_PACKAGE_PREFIX}-gst-plugins-bad-libs=$pkgver"
    "${MINGW_PACKAGE_PREFIX}-aom"
    "${MINGW_PACKAGE_PREFIX}-bzip2"
    "${MINGW_PACKAGE_PREFIX}-cairo"
    "${MINGW_PACKAGE_PREFIX}-chromaprint"
    "${MINGW_PACKAGE_PREFIX}-curl"
    "${MINGW_PACKAGE_PREFIX}-faad2"
    "${MINGW_PACKAGE_PREFIX}-faac"
    "${MINGW_PACKAGE_PREFIX}-fdk-aac"
    "${MINGW_PACKAGE_PREFIX}-fluidsynth"
    "${MINGW_PACKAGE_PREFIX}-gsm"
    "${MINGW_PACKAGE_PREFIX}-lcms2"
    "${MINGW_PACKAGE_PREFIX}-libass"
    "${MINGW_PACKAGE_PREFIX}-libbs2b"
    "${MINGW_PACKAGE_PREFIX}-libdca"
    "${MINGW_PACKAGE_PREFIX}-libdvdnav"
    "${MINGW_PACKAGE_PREFIX}-libdvdread"
    "${MINGW_PACKAGE_PREFIX}-libexif"
    "${MINGW_PACKAGE_PREFIX}-libgme"
    "${MINGW_PACKAGE_PREFIX}-libjpeg"
    "${MINGW_PACKAGE_PREFIX}-libmodplug"
    "${MINGW_PACKAGE_PREFIX}-libmpeg2"
    "${MINGW_PACKAGE_PREFIX}-libnice"
    "${MINGW_PACKAGE_PREFIX}-librsvg"
    "${MINGW_PACKAGE_PREFIX}-libsndfile"
    "${MINGW_PACKAGE_PREFIX}-libsrtp"
    "${MINGW_PACKAGE_PREFIX}-libssh2"
    "${MINGW_PACKAGE_PREFIX}-libwebp"
    "${MINGW_PACKAGE_PREFIX}-libxml2"
    "${MINGW_PACKAGE_PREFIX}-libmicrodns"
    "${MINGW_PACKAGE_PREFIX}-libmpcdec"
    "${MINGW_PACKAGE_PREFIX}-nettle"
    "${MINGW_PACKAGE_PREFIX}-openal"
    "${MINGW_PACKAGE_PREFIX}-openexr"
    "${MINGW_PACKAGE_PREFIX}-openh264"
    "${MINGW_PACKAGE_PREFIX}-openjpeg2"
    "${MINGW_PACKAGE_PREFIX}-openssl"
    "${MINGW_PACKAGE_PREFIX}-opus"
    "${MINGW_PACKAGE_PREFIX}-orc"
    "${MINGW_PACKAGE_PREFIX}-pango"
    "${MINGW_PACKAGE_PREFIX}-rtmpdump"
    "${MINGW_PACKAGE_PREFIX}-soundtouch"
    "${MINGW_PACKAGE_PREFIX}-spandsp"
    "${MINGW_PACKAGE_PREFIX}-srt"
    "${MINGW_PACKAGE_PREFIX}-vo-amrwbenc"
    "${MINGW_PACKAGE_PREFIX}-webrtc-audio-processing"
    "${MINGW_PACKAGE_PREFIX}-x265"
    "${MINGW_PACKAGE_PREFIX}-zbar"
  )
  cd "${srcdir}/build-${MSYSTEM}"

  mkdir -p "${pkgdir}${MINGW_PREFIX}"
  mv _plugins/* "${pkgdir}${MINGW_PREFIX}"
}

package_gst-plugin-opencv() {
  pkgdesc+=" (opencv plugin)"
  depends=(
    "${MINGW_PACKAGE_PREFIX}-gst-plugins-base"
    "${MINGW_PACKAGE_PREFIX}-opencv"
  )

  cd "${srcdir}/build-${MSYSTEM}"
  mkdir -p "${pkgdir}${MINGW_PREFIX}"
  mv plugin-opencv/* "${pkgdir}${MINGW_PREFIX}"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
